package com.example.mycityapp;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONException;
import org.json.JSONObject;

import android.R.string;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/*
 * Link example:
 * http://titan.cmpe.boun.edu.tr:8087/MyCity/services/user/showAllPosts?userId=1
 */

public class MainActivity extends Activity {
	public static MainActivity instance;
	public static String userId = "";
	public static String firstName = "";
	public static String lastName = "";
	public static Boolean dontLoadImages = false;//THIS VAR IS CREATED FOR FASTER TESTING PURPOSES 
												//MAKE THIS FALSE IF YOU WANT TO SEE (DOWNLOAD) IMAGES

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {    
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		instance = MainActivity.this;
		if (Build.VERSION.SDK_INT >= 9) {
		    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		    StrictMode.setThreadPolicy(policy);
		}
		
		EditText usernameEditText = (EditText) findViewById(R.id.userNameEditText);
		EditText passwordEditText = (EditText) findViewById(R.id.passwordEditText);
		/*usernameEditText.setText("talha@gmail.com");
		passwordEditText.setText("t");*/

		Button b = (Button) findViewById(R.id.loginButton);
		b.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EditText usernameEditText = (EditText) findViewById(R.id.userNameEditText);
				EditText passwordEditText = (EditText) findViewById(R.id.passwordEditText);

				String username = usernameEditText.getText().toString();
				String password = passwordEditText.getText().toString();
				String url = "http://titan.cmpe.boun.edu.tr:8087/MyCity/services/user/login?email="
						+ username + "&password=" + password;
				
				
				JSONParser jParser = new JSONParser();
				
				JSONObject userInformation = null;
				try {
					userInformation = new JSONObject(jParser.doInBackground(url));
				} catch (JSONException e1) {
					e1.printStackTrace();
				}
				Boolean nullUser = false;
				if(userInformation != null){
					try {
						userId = userInformation.getString("userId");
						firstName = userInformation.getString("firstName");
						nullUser = (firstName == "null");
						lastName = userInformation.getString("lastName");
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}	
				}
				if (userInformation == null || nullUser){
	                AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(MainActivity.this);

                    dlgAlert.setMessage("wrong password or username");
                    dlgAlert.setTitle("Error Message...");
                    dlgAlert.setPositiveButton("OK", null);
                    dlgAlert.setCancelable(true);
                    dlgAlert.create().show();

                    dlgAlert.setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {

                        }
                    });
				}
				else
				{
					startActivity(new Intent(MainActivity.this, HomePage.class));
				}

				

			}
		});
		Button b2 = (Button) findViewById(R.id.registerButton);
		b2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(MainActivity.this, RegisterPage.class));
			}
			
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
